﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL;
using System.Data;
namespace BAL
{
    public class BAL_CHITIETSUDUNGDV
    {
        public static QuanLyTiemNetDataContext db = new QuanLyTiemNetDataContext();
        //Them
        public static bool ThemCTSDDV(ref string err,string MaDV,int MaPhieu,int SoLuong,float DonGia)
        {
            bool them = false;
            try
            {
                CHITIETSUDUNGDV ctdv = new CHITIETSUDUNGDV();
                ctdv.MaDV = MaDV;
                ctdv.MaPhieu = MaPhieu;
                ctdv.SoLuong = SoLuong;
                ctdv.DonGia = DonGia;
                ctdv.DaXoa = false;
                db.CHITIETSUDUNGDVs.InsertOnSubmit(ctdv);
                db.SubmitChanges();
                them = true;
            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return them;
        }
        //Sua
        public static bool SuaCTSDDV(ref string err,string MaDV,int MaPhieu,int SoLuong,float DonGia)
        {
            bool sua = false;
            try
            {
                CHITIETSUDUNGDV tim = db.CHITIETSUDUNGDVs.Single(ctdv => ctdv.MaDV.Equals(MaDV) && ctdv.MaPhieu.Equals(MaPhieu));
                if(tim!=null)
                {
                    tim.SoLuong = SoLuong;
                    tim.DonGia = DonGia;
                    db.SubmitChanges();
                    sua = true;
                }

            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return sua;
        }
        //Xoa
        public static bool XoaCTSDDV(ref string err,string MaDV,int MaPhieu)
        {
            bool xoa = false;
            try
            {
                CHITIETSUDUNGDV tim = db.CHITIETSUDUNGDVs.Single(ctdv => ctdv.MaDV.Equals(MaDV) && ctdv.MaPhieu.Equals(MaPhieu));
                if (tim != null)
                {
                    db.CHITIETSUDUNGDVs.DeleteOnSubmit(tim);
                    db.SubmitChanges();
                    xoa = true;
                }
            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return xoa;
        }
        //đánh dấu xóa
        public static bool DanhDauXoa(ref string err,string MaDV,int MaPhieu,bool value)
        {
            bool danhdauxoa = false;
            try
            {
                CHITIETSUDUNGDV tim = db.CHITIETSUDUNGDVs.Single(ctdv => ctdv.MaDV.Equals(MaDV) && ctdv.MaPhieu.Equals(MaPhieu));
                if (tim != null)
                {
                    tim.DaXoa = value;
                    db.SubmitChanges();
                    danhdauxoa = true;
                }
            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return danhdauxoa;
        }
        public static DataTable ConvertToDataTable(List<CHITIETSUDUNGDV> list)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("MaDV");
            dt.Columns.Add("MaPhieu");
            dt.Columns.Add("SoLuong");
            dt.Columns.Add("DonGia");
            dt.Columns.Add("DaXoa");
            foreach(var item in list)
            {
                dt.Rows.Add(item.MaDV, item.MaPhieu, item.SoLuong, item.DonGia,item.DaXoa);
            }
            return dt;
        }
        public static DataTable LayThongTinCTSDDV()
        {
            var ctdv = from t in db.CHITIETSUDUNGDVs select t;
            return ConvertToDataTable(ctdv.ToList());
        }
        //xem chi tiet su dung dich vu của một máy
        public static DataTable XemChiTietSuDungDichVuMay(int MaPhieu)
        {
            var ctdv = from t in db.CHITIETSUDUNGDVs where t.MaPhieu.Equals(MaPhieu) select t;
            return ConvertToDataTable(ctdv.ToList());
        }
    }
}
